package com.group3.controller;/**
 * Download
 *
 * @author HY
 * 2021/07/17 9:00
 **/


import com.group3.config.MyControllerAdvice;
import com.group3.service.DownloadService;
import com.group3.utils.DownloadUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Description 下载文件接口
 * @author 余久
 * @Date 2021/07/17 9:00
 **/


@Controller
@RequestMapping("/file")
@Api(tags = "下载文件接口")
public class Download {

    private final static Logger LOGGER = LoggerFactory.getLogger(MyControllerAdvice.class);

    @Autowired
    private DownloadService downloadService;
    

    /**
     * @Description 头像
     * @Author HY
     * @Date 2021/07/17 11:03
     * @param fileName
     * @param request
     * @param response
     * @return: void
     **/
    @GetMapping(value = "/HeadPortrait/{fileName}")
    public void downloadHeadPortrait(@PathVariable String fileName,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) {

        Resource resource = downloadService.loadFileAsResource("HeadPortrait/" + fileName);
        DownloadUtil.Download(resource, request, response);
    }
    /**
     * @Description 下载作业附件
     * @Author HY
     * @Date 2021/07/17 11:03
     * @param fileName
     * @param request
     * @param response
     * @return: void
     **/
    @GetMapping(value = "/HomeWork/{fileName}")
    public void downloadHomeWork(@PathVariable String fileName,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) {

        Resource resource = downloadService.loadFileAsResource("HomeWork/" + fileName);
        DownloadUtil.Download(resource, request, response);
    }

    /**
     * @Description 学生作业附件下载
     * @Author HY
     * @Date 2021/07/17 11:04
     * @param fileName
     * @param request
     * @param response
     * @return: void
     **/
    @GetMapping(value = "/HomeworkAttachment/{fileName}")
    public void downloadHomeworkAttachment(@PathVariable String fileName,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) {

        Resource resource = downloadService.loadFileAsResource("HomeworkAttachment/" + fileName);
        DownloadUtil.Download(resource, request, response);
    }

    /**
     * @Description 学生上传请假附件
     * @Author HY
     * @Date 2021/07/17 11:05
     * @param fileName
     * @param request
     * @param response
     * @return: void
     **/
    @GetMapping(value = "/LeaveAttachment/{fileName}")
    public void downloadLeaveAttachment(@PathVariable String fileName,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) {

        Resource resource = downloadService.loadFileAsResource("LeaveAttachment/" + fileName);
        DownloadUtil.Download(resource, request, response);
    }


    @GetMapping(value = "/ClockInTheTemplate/Data/{fileName}")
    public void downloadClockInTheTemplateData(@PathVariable String fileName,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) {

        Resource resource = downloadService.loadFileAsResource("ClockInTheTemplate/Data/" + fileName);
        DownloadUtil.Download(resource, request, response);
    }
}
